package cn.itcast.goods.admin.admin.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.admin.admin.domain.Admin;
import cn.itcast.goods.admin.admin.service.AdminService;
import cn.itcast.servlet.BaseServlet;

public class AdminServlet extends BaseServlet {

	private AdminService adminService = new AdminService();
	/**
	 * 管理登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Admin from = CommonUtils.toBean(request.getParameterMap(), Admin.class);
		Admin admin = adminService.login(from.getAdminname(),from.getAdminpwd());
		if(admin != null)
		{
			request.getSession().setAttribute("admin", admin);
			return "r:/adminjsps/admin/index.jsp";
		}
		else
		{
			request.setAttribute("msg", "用户名或密码错误！");
			request.setAttribute("from",from);
			return "f:/adminjsps/login.jsp";
		}
	     
	}

	/**
	 * 管理员退出
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String quit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().removeAttribute("admin");
		return "r:/adminjsps/login.jsp";
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}
